﻿using Common.Log;
using DryWCS.App.Common;
using DryWCS.App.View.Tasks;
using DryWCS.App.ViewModel.Base;
using DryWCS.Model.GlobalPara;
using DryWCS.Model.Stacker;
using DryWCS.Service.Stacker;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DryWCS.App.ViewModel.Tasks
{
    public class ExcStackerTaskModel : DataProcess<StackerTaskEntity>
    {
        #region  Parameter
        string msg = null;
        StackerTaskService stackerTaskService = new StackerTaskService();
        public List<StackerTaskEntity> models;
        #endregion

        #region Constructor
        public  ExcStackerTaskModel()
        {
            GridModelList = new ObservableCollection<StackerTaskEntity>();
            QueryStackerTask();
            ShowAddTaskViewCommand = new RelayCommand(ShowAddTaskView);
            ReSendOrderCommand = new RelayCommand<StackerTaskEntity>(ReSendOrder);
            DelOrderCommand = new RelayCommand<StackerTaskEntity>(DelOrder);
            QueryStackerTaskCommand = new RelayCommand(QueryStackerTask);
            QueryConveyorTaskCommand = new RelayCommand(QueryConveyorTask);
        }
        #endregion

        #region Command
        public RelayCommand ShowAddTaskViewCommand { get; set; }
        public RelayCommand QueryStackerTaskCommand { get; set; }

        public RelayCommand QueryConveyorTaskCommand { get; set; }
        public RelayCommand<StackerTaskEntity> ReSendOrderCommand { get; set; }
        public RelayCommand<StackerTaskEntity> DelOrderCommand { get; set; }
        #endregion

        #region Add stacker task info method 
        private void ShowAddTaskView()
        {
            AddStackerTaskView addTaskView = new AddStackerTaskView();
            addTaskView.ShowDialog();
        }
        #endregion

        #region Query stacker task 
        private void  QueryStackerTask()
        {
            models = stackerTaskService.GetList().OrderByDescending(it=>it.Id).ToList();
           
            GridModelList.Clear();

            if (models != null)
            {
                if (models.Count == 0)
                {
                    Msg.Info("未检索到匹配数据！");
                }

                else
                {
                    models.ForEach(m =>
                    {
                        GridModelList.Add(m);
                    }
                        );
                }

            }
            TotalCount = GridModelList.Count;
            GetPageData(PageIndex);
        }
        #endregion

        #region Query conveyor task
        private void QueryConveyorTask()
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Add delete  edit
        private void AddModel()
        {
            if (Model == null)
            {
                Msg.Warning("请输入添加内容！");

            }
            msg = CheckHelper.CheckNullable(Model);
            if (msg != null)
            {
                Msg.Warning(msg);
                return;
            }
            else
            {
                if (stackerTaskService.Insert(Model))
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname} 插入数据{Model.StartLineNo}_{Model.StartColNo}_{Model.StartLayerNo} to {Model.EndLineNo}_{Model.EndColNo}_{Model.EndLayerNo}成功";
                }
                else
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname} 插入数据{Model.StartLineNo}_{Model.StartColNo}_{Model.StartLayerNo} to {Model.EndLineNo}_{Model.EndColNo}_{Model.EndLayerNo}失败";
                }
                Msg.Info(msg);
                LogHelper.WriteLog(msg);
                QueryStackerTask();
                //QueryModels();
            }
        }
        void DelOrder(StackerTaskEntity model)
        {
            if (model == null)
            {
                Msg.Info("请选择要删除的数据");
            }
            if (Msg.Question($"是否要删除{model.Id} 本条数据?"))
            {
                if (stackerTaskService.Delete(model))
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname} 删除{model.Id},{model.StartLineNo}_{model.StartColNo}_{model.StartLayerNo} to {model.EndLineNo}_{model.EndColNo}_{model.EndLayerNo}数据成功";
                }
                else
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname} 删除{model.Id},{model.StartLineNo}_{model.StartColNo}_{model.StartLayerNo} to {model.EndLineNo}_{model.EndColNo}_{model.EndLayerNo}数据成功";
                }
                Msg.Info(msg);
                LogHelper.WriteLog(msg);
            }
            // Messenger.Default.Register<bool>(this, "Del", DeleteInfo);
            QueryStackerTask();


        }
        
        #endregion

        #region Resend command
        private void ReSendOrder(StackerTaskEntity model)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region 分页处理
        public override void GetPageData(int pageIndex)
        {
            try
            {
                Application.Current?.Dispatcher.Invoke(() =>
                {
                    //PageSize = 5;
                    TotalCount = models.Count;
                    GridModelList.Clear();
                    models.Skip((pageIndex - 1) * PageSize).Take(PageSize).ToList().ForEach(p => GridModelList.Add(p));
                });

                base.SetPageCount();
            }
            catch (Exception ex)
            {
                Msg.Error(ex.Message);
            }
        }
        #endregion

    }
}
